package com.jinxin.platform.cdinfogo.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jinxin.platform.cdinfogo.mapper.CdinfogoDeviceMapper;
import com.jinxin.platform.cdinfogo.pojo.domain.CdinfogoDevice;
import com.jinxin.platform.cdinfogo.pojo.vo.DeviceContVo;
import com.jinxin.platform.cdinfogo.pojo.vo.WeatherInfo;
import com.jinxin.platform.cdinfogo.rpc.CdInfoGoRpcService;
import com.jinxin.platform.cdinfogo.utils.URLEncoder;
import com.jinxin.platform.cdinfogo.utils.WeatherUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author : zhangpeng
 * @date : 2021-07-02 14:10:57
 **/
@Slf4j
@Configuration
@EnableScheduling
public class CdinfogoScheduleTask {

    @Resource
    private CdinfogoDeviceMapper cdinfogoDeviceMapper;
    @Resource
    private CdInfoGoRpcService cdInfoGoRpcService;

    @Scheduled(cron = "0 0/1 * * * ?")
    private void countLotTasks() {
        List<CdinfogoDevice> cdinfogoDevices = cdinfogoDeviceMapper.queryAll(null);

        cdinfogoDevices.forEach( cdinfogoDevice -> {
            List<DeviceContVo> deviceContVos = cdinfogoDeviceMapper.queryCentByDeviceId(cdinfogoDevice.getId());
            deviceContVos.forEach( cent -> {
                if("2".equals(cent.getShowType())){
                    Integer items = cdInfoGoRpcService.getItems(cent.getContent());
                    String p2 = (cent.getShowFont() == null ? "" : cent.getShowFont())
                            + (cent.getShowColor() == null ? "" : cent.getShowColor())
                            + (cent.getContent() == null ? "" : cent.getContent()) + "　" + items;
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("p1",cent.getShowArea());
                    jsonObject.put("p2",p2);
                    String s = jsonObject.toJSONString();
                    String s1 = null;
                    try {
                        s1 = URLEncoder.encodeURI(s);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    cdInfoGoRpcService.controlDevice(cdinfogoDevice,s1,"Distribution content");
                }else {
                    if(!StringUtils.isBlank(cent.getOther())){
                        WeatherInfo wea = WeatherUtils.GetWeatherData(cent.getOther());
                        String content = wea.getTemperature() + "℃　" + wea.getHumidity() + "%　" + wea.getWindDirection() + "风　" + wea.getWeather();
                        cent.setContent(content);
                    }
                    String p2 = (cent.getShowFont() == null ? "" : cent.getShowFont())
                            + (cent.getShowColor() == null ? "" : cent.getShowColor())
                            + (cent.getContent() == null ? "" : cent.getContent());
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("p1",cent.getShowArea());
                    jsonObject.put("p2",p2);
                    String s = jsonObject.toJSONString();
                    String s1 = null;
                    try {
                        s1 = URLEncoder.encodeURI(s);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    cdInfoGoRpcService.controlDevice(cdinfogoDevice,s1,"Distribution content");
                }

            });
        });
    }

//    @Scheduled(cron = "0 0/10 * * * ?")
//    private void weatherTasks() {
//        CdinfogoDevice cdinfogoDevice1 = new CdinfogoDevice();
//        cdinfogoDevice1.setDeviceType("1");
//        List<CdinfogoDevice> cdinfogoDevices = cdinfogoDeviceMapper.queryAll(cdinfogoDevice1);
//
//        cdinfogoDevices.forEach( cdinfogoDevice -> {
//            List<DeviceContVo> deviceContVos = cdinfogoDeviceMapper.queryCentByDeviceId(cdinfogoDevice.getId());
//            deviceContVos.forEach( cent -> {
//                WeatherInfo wea = WeatherUtils.GetWeatherData(cent.getOther());
//                String content = wea.getTemperature() + "℃　" + wea.getHumidity() + "%　" + wea.getWindDirection() + "风　" + wea.getWeather();
//
//            });
//        });
//    }

}
